<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="460px" :before-close="handleCancel">
    <el-form ref="formDate" label-suffix=":" label-width="100px" :rules="rules" :model="form">
      <el-row>
        <el-col :span="24">
          <el-form-item label="项目" prop="pro_id" style="width: 100%">
            <el-select
              v-model="form.pro_id"
              size="small"
              :remote-method="selPros"
              remote
              filterable
              style="width: 100%"
              placeholder="请选择项目名称"
              clearable
              @focus="selPros"
            >
              <el-option
                v-for="item in pors"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="施工人员" prop="" style="width: 100%">
            <el-button class="fr" type="primary" size="mini" @click="handleAdd">新 增</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="" prop="user" style="width: 100%">
            <el-tag
              class="mr10 mb5"
              v-for="tag in list"
              :key="tag.key"
              @close="handleDelete(tag)"
              closable>
              {{tag.value}}
            </el-tag>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="项目负责人" prop="first" style="width: 100%">
            <el-select v-model="form.first" size="small" style="width: 100%">
              <el-option
                v-for="item in list"
                :key="item.key"
                :label="item.value"
                :value="item.key"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark" style="width: 100%">
            <el-input type="textarea" :rows="4" v-model="form.remark" size="small" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="fr mt10">
            <el-button size="small" @click="handleCancel()">取 消</el-button>
            <el-button type="primary" size="small" @click="handleEdit()">保 存</el-button>
          </div>
        </el-col>
      </el-row>
    </el-form>
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
  </el-dialog>
</template>

<script>
import { selPros, addProstaff } from '@/api/project'
import Dialog from './staff-dialog'

export default {
  components: { Dialog },
  inject: ['app'],
  props: {
    info: {
      type: Object,
      default: null
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  data() {
    return {
      pors: [],
      list: [],
      form: {},
      types: [{key: '0', value: '否'}, {key: '1', value: '是'}],
      rules: {
        first: [{ required: true, trigger: 'blur', message: '项目负责人不可为空' }],
        user: [{ required: true, trigger: 'blur', message: '施工人员不可为空' }],
        pro_id: [{ required: true, trigger: 'blur', message: '项目不可为空' }]
      },
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      visible: false
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.$set(this, 'form', this.info.edit)
        this.$set(this, 'list', [])
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    getList(e){
      let js = {}
      this.list.forEach(o => {
        js[o.key] = 1
      })
      if(!js[e.key]) this.list.push(e)
      this.$set(this.form, 'user', this.list)
    },
    handleDelete(e) {
      let len = this.list.length
      let i = 0
      for(i = 0; i < len; i++) {
        let o = this.list[i]
        if(o.key === e.key) break
      }
      this.list.splice(i, 1)
      this.$set(this.form, 'user', this.list)
    },
    selPros(query) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selPros({ name: str }).then(response => {
        const pros = response.pros
        this.pors = pros
      }).catch(err => {
        this.$message.error(err)
      })
    },
    handleEdit() {
      this.$refs['formDate'].validate(async(valid) => {
        if (valid) {
          let data = {}
          data.director = this.form.first
          data.pro_id = this.form.pro_id
          let arr = []
          this.list.forEach(o => {
            arr.push(o.key)
          })
          data.staff_ids = arr.join()
          data.remark = this.form.remark
          addProstaff(data).then(response => {
            this.getData()
            this.handleCancel()
            this.$message.success('添加成功')
          }).catch(err => {
            this.$message.error(err.msg)
          })
        }
      })
    },
    handleAdd(e) {
      this.dialog.title = '新增人员'
      this.dialog.type = 'add'
      e = {}
      this.app.dialogParam(this.dialog.edit, ['user'], e)
      this.$refs['dialog'].visible = true
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    }
  }
}
</script>

<style>

</style>
